import {
  Body,
  Controller,
  Get,
  Inject,
  Param,
  ParseIntPipe,
  Post,
  Query,
  UseGuards,
  Request,
  Delete,
} from '@nestjs/common';
import { UserService } from './user.service';
import {
  AddUserRolesDto,
  CreateUserDto,
  GetUserDto,
  RegisterUserDto,
  UpdatePasswordDto,
  UpdateUserDto,
} from '@/modules/user/dto/dto';
import { EmailService } from '@/modules/email/email.service';
import { RedisService } from '@/shared/redis.service';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { JwtGuard } from '@/common/guards/jwt.guard';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Inject(EmailService)
  private emailService: EmailService;

  @Inject(RedisService)
  private redisService: RedisService;

  @Post('register')
  async register(@Body() registerUser: RegisterUserDto) {
    return await this.userService.register(registerUser);
  }

  @Get('register-captcha')
  async captcha(@Query('address') address: string) {
    const code = Math.random().toString().slice(2, 8);

    await this.redisService.set(`captcha_${address}`, code, 5 * 60);

    await this.emailService.sendMail({
      to: address,
      subject: '注册验证码',
      html: `<p>你的注册验证码是 ${code}</p>`,
    });
    return '发送成功';
  }

  //发送修改密码验证码
  @Get('update_password/captcha')
  async updatePasswordCaptcha(@Query('address') address: string) {
    const code = Math.random().toString().slice(2, 8);

    await this.redisService.set(`update_password_captcha_${address}`, code, 10 * 60);

    await this.emailService.sendMail({
      to: address,
      subject: '更改密码验证码',
      html: `<p>你的更改密码验证码是 ${code}</p>`,
    });
    return '发送成功';
  }

  //发送修改用户信息验证码
  @Get('update/captcha')
  async updateCaptcha(@Query('address') address: string) {
    const code = Math.random().toString().slice(2, 8);

    await this.redisService.set(`update_user_captcha_${address}`, code, 10 * 60);

    await this.emailService.sendMail({
      to: address,
      subject: '更改用户信息验证码',
      html: `<p>你的验证码是 ${code}</p>`,
    });
    return '发送成功';
  }

  // 创建用户
  @Post('create')
  @UseGuards(JwtGuard) // 需要鉴权
  createUser(@Body() user: CreateUserDto) {
    return this.userService.createUser(user);
  }

  // 账号状态切换
  @Post('switchAccountStatus/:id')
  changUserStatus(@Param('id', ParseIntPipe) id: number) {
    return this.userService.switchAccountStatus(id);
  }

  // 获取用户详情 - 用户接口-根据token
  @Get('userInfo')
  @UseGuards(JwtGuard)
  getUserInfo(@Request() req: any) {
    return this.userService.getUserInfo(req.user);
  }

  // 修改用户资料
  @Post('updateUser/:id')
  @UseGuards(JwtGuard) // 需要鉴权
  updateUser(@Param('id', ParseIntPipe) id: number, @Body() user: UpdateUserDto) {
    return this.userService.updateUser(id, user);
  }

  // 下线用户
  @Post('offlineUser/:id')
  @UseGuards(JwtGuard) // 需要鉴权
  offlineUser(@Param('id', ParseIntPipe) id: number) {
    return this.userService.offlineUser(id);
  }

  // 获取用户列表
  @Get()
  @UseGuards(JwtGuard) // 需要鉴权
  getUserList(@Query() queryDto: GetUserDto) {
    return this.userService.getUserList(queryDto);
  }

  // 给用户分配角色
  @Post('updateUserRoles/:id')
  @UseGuards(JwtGuard) // 需要鉴权
  updateUserRoles(@Param('id', ParseIntPipe) id: number, @Body() rolesDto: AddUserRolesDto) {
    return this.userService.updateUserRoles(id, rolesDto);
  }

  // 重置用户密码
  @Post('reset/password/:id')
  @UseGuards(JwtGuard) // 需要鉴权
  resetPassword(@Param('id', ParseIntPipe) id: number, @Body() password: UpdatePasswordDto) {
    return this.userService.resetPassword(id, password);
  }

  // 删除用户/员工
  @Delete(':id')
  @UseGuards(JwtGuard) // 需要鉴权
  deleteUser(@Param('id', ParseIntPipe) id: number) {
    return this.userService.deleteUser(id);
  }

  // 获取系统当前在线用户列表
  @Get('online/list')
  getOnlineUserList() {
    return this.userService.getOnlineUserList();
  }
}
